/* 处理提交保存的数据方法 */
export const getDealBaseDataToApiFn = (data) => {
  let {
    tableData,
    type
  } = data
  let valueName = type === 'add' ? 'valueName' : 'valueNameUpdate'
  let valueUuid = type === 'add' ? 'valueUuid' : 'valueUuidUpdate'
  let uuidName = type === 'add' ? 'productUuid' : 'productKey'
  let isEmptyItemNameList = new Set()
  let submitResults = _.cloneDeep(tableData).reduce((value, item) => {
    let notSpecList = item.attrNameList && item.attrNameList.map(attrItem => {
      if (item[attrItem].attrMust === '1' && !item[attrItem][valueName]) {
        isEmptyItemNameList.add(item[attrItem].attrName)
      }
      if (item[attrItem].attrType === '04') {
        const valueNames = []
        const valueUuids = []
        const nameList = item[attrItem][valueName] ?? []
        nameList.forEach(name => {
          const opt = item[attrItem].allValueOptions.find(item => item.optionHtml == name)
          opt && valueNames.push(opt.optionHtml)
          opt && valueUuids.push(opt.optionValue)
        })
        item[attrItem][valueName] = valueNames.join(',')
        item[attrItem][valueUuid] = valueUuids.join(',')
      }
      return item[attrItem]
    })
    value.push({
      [uuidName]: item.productUuid,
      notSpecList
    })
    return value
  }, [])
  let result = {
    submitResults
  }
  if (isEmptyItemNameList.size !== 0) {
    result.errorMessage = `${[...isEmptyItemNameList].join(',')}不能为空`
  }
  return result
}
